home *** CD-ROM | disk | FTP | other *** search
/ CD World Haziran 1997 / CD World Haziran 1997.iso / Cesitlemeler / Directx 3.0 / dx3.exe / SDK / SAMPLES / DUEL / COMM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-04  |  7.7 KB  |  353 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995-1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File:       comm.c
  6.  *  Content:    DirectPlay related code
  7.  *
  8.  *
  9.  ***************************************************************************/
  10. #include "comm.h"
  11. #include "lobby.h"
  12.  
  13. /*
  14.  * Externals
  15.  */
  16. extern LPGUID                       glpGuid;            // duel's guid
  17. extern LPDPLCONNECTION              glpdplConnection;   // connection settings
  18.  
  19. /*
  20.  * Globals
  21.  */
  22. LPDPSESSIONDESC2                    glpdpSD;            // current session description
  23. LPDIRECTPLAY2                       glpDP=NULL;     // directplay object pointer
  24.  
  25. /*
  26.  * DPlayClose
  27.  *
  28.  * Wrapper for DirectPlay Close API
  29.  */
  30. HRESULT DPlayClose(void)
  31. {
  32.     HRESULT hr=E_FAIL;
  33.  
  34.     if (glpDP) 
  35.         hr = IDirectPlay2_Close(glpDP);
  36.     
  37.     return hr;
  38. }
  39.  
  40. /*
  41.  * DPlayCreate
  42.  *
  43.  * Wrapper for DirectPlay Create API. Retrieves a DirectPlay2/DirectPlay2A interface
  44.  * based on the UNICODE flag
  45.  * 
  46.  */
  47. HRESULT DPlayCreate(LPGUID lpGuid)
  48. {
  49.     HRESULT hr=E_FAIL;
  50.     LPDIRECTPLAY lpDP=NULL;
  51.  
  52.     // create a DirectPlay1 interface
  53.     if ((hr = DirectPlayCreate(lpGuid, &lpDP, NULL)) == DP_OK)
  54.     {
  55.         if (lpDP)
  56.         {
  57.             // query for a DirectPlay2(A) interface
  58. #ifdef UNICODE
  59.             hr = IDirectPlay_QueryInterface(lpDP,&IID_IDirectPlay2,(LPVOID *)&glpDP);
  60. #else
  61.             hr = IDirectPlay_QueryInterface(lpDP,&IID_IDirectPlay2A,(LPVOID *)&glpDP);
  62. #endif
  63.             // no longer need the DirectPlay1 interface
  64.             IDirectPlay_Release(lpDP);
  65.         }
  66.     }
  67.  
  68.     return hr;
  69. }
  70.  
  71. /*
  72.  * DPlayCreatePlayer
  73.  *
  74.  * Wrapper for DirectPlay CreatePlayer API. 
  75.  */
  76.  
  77. HRESULT DPlayCreatePlayer(LPDPID lppidID, LPTSTR lptszPlayerName, HANDLE hEvent, 
  78.                           LPVOID lpData, DWORD dwDataSize)
  79. {
  80.     HRESULT hr=E_FAIL;
  81.     DPNAME name;
  82.     
  83.     ZeroMemory(&name,sizeof(name));
  84.     name.dwSize = sizeof(DPNAME);
  85.  
  86. #ifdef UNICODE
  87.     name.lpszShortName = lptszPlayerName;
  88. #else
  89.     name.lpszShortNameA = lptszPlayerName;
  90. #endif
  91.  
  92.     if (glpDP)
  93.         hr = IDirectPlay2_CreatePlayer(glpDP, lppidID, &name, hEvent, lpData, 
  94.                                       dwDataSize, 0);
  95.                                     
  96.     return hr;
  97. }
  98.  
  99. /*
  100.  * DPlayCreateSession
  101.  *
  102.  * Wrapper for DirectPlay CreateSession API.Uses the global application guid (glpGuid).
  103.  */
  104. HRESULT DPlayCreateSession(LPTSTR lptszSessionName)
  105. {
  106.     HRESULT hr = E_FAIL;
  107.     DPSESSIONDESC2 dpDesc;
  108.  
  109.     ZeroMemory(&dpDesc, sizeof(dpDesc));
  110.     dpDesc.dwSize = sizeof(dpDesc);
  111.     dpDesc.dwFlags = DPSESSION_MIGRATEHOST | DPSESSION_KEEPALIVE;
  112.  
  113. #ifdef UNICODE
  114.     dpDesc.lpszSessionName = lptszSessionName;
  115. #else
  116.     dpDesc.lpszSessionNameA = lptszSessionName;
  117. #endif
  118.  
  119.     // set the application guid
  120.     if (glpGuid)
  121.         dpDesc.guidApplication = *glpGuid;
  122.  
  123.     if (glpDP)
  124.         hr = IDirectPlay2_Open(glpDP, &dpDesc, DPOPEN_CREATE);
  125.  
  126.     return hr;
  127. }
  128.  
  129. /*
  130.  * DPlayDestroyPlayer
  131.  * 
  132.  * Wrapper for DirectPlay DestroyPlayer API. 
  133.  */
  134. HRESULT DPlayDestroyPlayer(DPID pid)
  135. {
  136.     HRESULT hr=E_FAIL;
  137.     
  138.     if (glpDP)
  139.         hr = IDirectPlay2_DestroyPlayer(glpDP, pid);
  140.  
  141.     return hr;
  142. }
  143.  
  144. /*
  145.  * DPlayEnumPlayers
  146.  *
  147.  * Wrapper for DirectPlay API EnumPlayers
  148.  */
  149. HRESULT DPlayEnumPlayers(LPGUID lpSessionGuid, LPDPENUMPLAYERSCALLBACK2 lpEnumCallback, 
  150.                          LPVOID lpContext, DWORD dwFlags)
  151. {
  152.     HRESULT hr=E_FAIL;
  153.  
  154.     if (glpDP)
  155.         hr = IDirectPlay2_EnumPlayers(glpDP, lpSessionGuid, lpEnumCallback, lpContext, dwFlags);
  156.  
  157.     return hr;
  158. }
  159.  
  160. /*
  161.  * DPlayEnumSessions
  162.  *
  163.  * Wrapper for DirectPlay EnumSessions API.
  164.  */
  165. HRESULT DPlayEnumSessions(DWORD dwTimeout, LPDPENUMSESSIONSCALLBACK2 lpEnumCallback, 
  166.                           LPVOID lpContext, DWORD dwFlags)
  167. {
  168.     HRESULT hr = E_FAIL;
  169.     DPSESSIONDESC2 dpDesc;
  170.  
  171.     ZeroMemory(&dpDesc, sizeof(dpDesc));
  172.     dpDesc.dwSize = sizeof(dpDesc);
  173.     if (glpGuid)
  174.         dpDesc.guidApplication = *glpGuid;
  175.  
  176.     if (glpDP)
  177.         hr = IDirectPlay2_EnumSessions(glpDP, &dpDesc, dwTimeout, lpEnumCallback,
  178.                                         lpContext, dwFlags);
  179.  
  180.  
  181.     return hr;
  182. }
  183.  
  184. /*
  185.  * DPlayGetPlayerData
  186.  * 
  187.  * Wrapper for DirectPlay GetPlayerData API.
  188.  */
  189. HRESULT DPlayGetPlayerData(DPID pid, LPVOID lpData, LPDWORD lpdwDataSize, DWORD dwFlags)
  190. {
  191.     HRESULT hr=E_FAIL;
  192.  
  193.     if (glpDP) 
  194.         hr = IDirectPlay2_GetPlayerData(glpDP, pid, lpData, lpdwDataSize, dwFlags);
  195.  
  196.     return hr;
  197. }
  198.  
  199. /*
  200.  * DPlayGetSessionDesc
  201.  *
  202.  * Wrapper for DirectPlay GetSessionDesc API. 
  203.  */
  204. HRESULT DPlayGetSessionDesc(void)
  205. {
  206.     HRESULT hr=E_FAIL;
  207.     DWORD dwSize;
  208.  
  209.     // free old session desc, if any
  210.     if (glpdpSD)
  211.     {
  212.         free(glpdpSD);
  213.         glpdpSD = NULL;
  214.     }
  215.  
  216.     if (glpDP)
  217.     {
  218.         // first get the size for the session desc
  219.         if ((hr = IDirectPlay2_GetSessionDesc(glpDP, NULL, &dwSize)) == DPERR_BUFFERTOOSMALL)
  220.         {
  221.             // allocate memory for it
  222.             glpdpSD = (LPDPSESSIONDESC2) malloc(dwSize);
  223.             if (glpdpSD)
  224.             {
  225.                 // now get the session desc
  226.                 hr = IDirectPlay2_GetSessionDesc(glpDP, glpdpSD, &dwSize);
  227.             }
  228.             else
  229.             {
  230.                 hr = E_OUTOFMEMORY;
  231.             }
  232.         }
  233.     }
  234.  
  235.     return hr;
  236. }
  237.  
  238. /*
  239.  * IsDPlay
  240.  *
  241.  * Returns TRUE if a DirectPlay interface exists, otherwise FALSE.
  242.  */
  243. BOOL IsDPlay(void)
  244. {
  245.     return (glpDP ? TRUE:FALSE);
  246. }
  247.  
  248. /*
  249.  * DPlayOpenSession
  250.  *
  251.  * Wrapper for DirectPlay OpenSession API. 
  252.  */
  253. HRESULT DPlayOpenSession(LPGUID lpSessionGuid)
  254. {
  255.     HRESULT hr = E_FAIL;
  256.     DPSESSIONDESC2 dpDesc;
  257.  
  258.     ZeroMemory(&dpDesc, sizeof(dpDesc));
  259.     dpDesc.dwSize = sizeof(dpDesc);
  260.  
  261.     // set the session guid
  262.     if (lpSessionGuid)
  263.         dpDesc.guidInstance = *lpSessionGuid;
  264.     // set the application guid
  265.     if (glpGuid)
  266.         dpDesc.guidApplication = *glpGuid;
  267.  
  268.     // open it
  269.     if (glpDP)
  270.         hr = IDirectPlay2_Open(glpDP, &dpDesc, DPOPEN_JOIN);
  271.  
  272.     return hr;
  273. }
  274.  
  275.  
  276. /*
  277.  * DPlayReceive
  278.  *
  279.  * Wrapper for DirectPlay Receive API
  280.  */
  281. HRESULT DPlayReceive(LPDPID lpidFrom, LPDPID lpidTo, DWORD dwFlags, LPVOID lpData, LPDWORD lpdwDataSize)
  282. {
  283.     HRESULT hr = E_FAIL;
  284.  
  285.     if (glpDP)
  286.         hr = IDirectPlay2_Receive(glpDP, lpidFrom, lpidTo, dwFlags, lpData, lpdwDataSize);
  287.     
  288.     return hr;
  289. }
  290.  
  291. /*
  292.  * DPlayRelease
  293.  *
  294.  * Wrapper for DirectPlay Release API.
  295.  */
  296. HRESULT DPlayRelease(void)
  297. {
  298.     HRESULT hr = E_FAIL;
  299.  
  300.     if (glpDP)
  301.     {
  302.         // free session desc, if any
  303.         if (glpdpSD) 
  304.         {
  305.             free(glpdpSD);
  306.             glpdpSD = NULL;
  307.         }
  308.  
  309.         // free connection settings structure, if any (lobby stuff)
  310.         if (glpdplConnection)
  311.         {
  312.             free(glpdplConnection);
  313.             glpdplConnection = NULL;
  314.         }
  315.         // release dplay
  316.         hr = IDirectPlay2_Release(glpDP);
  317.         glpDP = NULL;
  318.     }
  319.  
  320.     return hr;
  321. }
  322.  
  323. /*
  324.  * DPlaySend
  325.  * 
  326.  * Wrapper for DirectPlay Send API.
  327.  */
  328. HRESULT DPlaySend(DPID idFrom, DPID idTo, DWORD dwFlags, LPVOID lpData, DWORD dwDataSize)
  329. {
  330.     HRESULT hr = E_FAIL;
  331.  
  332.     if (glpDP)
  333.         hr = IDirectPlay2_Send(glpDP, idFrom, idTo, dwFlags, lpData, dwDataSize);
  334.     
  335.     return hr;
  336. }
  337.  
  338. /*
  339.  * DPlaySetPlayerData
  340.  *
  341.  * Wrapper for DirectPlay SetPlayerData API
  342.  */
  343. HRESULT DPlaySetPlayerData(DPID pid, LPVOID lpData, DWORD dwSize, DWORD dwFlags)
  344. {
  345.     HRESULT hr=E_FAIL;
  346.  
  347.     if (glpDP)
  348.         hr = IDirectPlay2_SetPlayerData(glpDP, pid, lpData, dwSize, dwFlags);
  349.     
  350.     return hr;
  351. }
  352.  
  353.